import $ from '../jquery-3.4.1.min.js';
import ajax from '../utils/ajax';



// 新增楼栋
$('#building .set-Building .addBuilding').click(function () {
    const community_id = $('#building #buildingCommunity').val();
    const buildingName = $('#building .buildingName').val();
    const buildingToward = $('#building .buildingToward').val();
    const buildingHeight = $('#building .buildingHeight').val();
    const buildingStructure = $('#building .buildingStructure').val();
    const buildingUnit = $('#building .buildingUnit').val();
    const buildingfloor = $('#building .buildingfloor').val();
    // console.log(
    //     community_id
    //     ,buildingName
    //     ,buildingToward
    //     ,buildingHeight
    //     ,buildingStructure
    //     ,buildingUnit
    //     ,buildingfloor
    //     );
    ajax({
        url: '/building',
        type: 'POST',
        data: {
            community_id//小区
            , buildingName//楼栋
            , buildingToward//朝向
            , buildingHeight//楼高
            , buildingStructure//楼宇结构
            , buildingUnit//最大单元数
            , buildingfloor//最大楼层
        },
        success(res) {
            console.log(res);
        }
    })
})

// 新增房屋获取小区，楼栋等数据
$('#building .building-content-nav span').click(function () {
    const spanText = $(this).text();
    if (spanText == '添加房屋') {
        $('.addHome').show();
        const community_id = $('#building #buildingCommunity').val();
        // console.log(community_id);
        ajax({
            url: '/building',
            type: 'GET',
            data: { community_id },
            success(res) {
                defaultSelect(res.result[0]);
            }
        })
    }
})

// 渲染select框
function defaultSelect(data) {
    console.log(data);
    const addHomeUnit = data.buildingUnit;
    const addHomefloor = data.buildingfloor;
    if (addHomeUnit > 0) {
        let j = 0;
        let html = '';
        for (let i = 0; i < addHomeUnit; i++) {
            html = html + `<option value="${j++}">${j}单元</option>`
        };
        $('#building .addHome .addHomeUnit').html(html);
    }
    if (addHomefloor > 0) {
        let k = 0;
        let html = '';
        for (let i = 0; i < addHomefloor; i++) {
            html = html + `<option value="${k++}">${k}层</option>`
        };
        $('#building .addHome .addHomefloor').html(html);
    }

}





// 新增房屋
$('#building .addHome .addBtn').click(function () {
    const homeUnit = Number($('#building .addHome .addHomeUnit').val()) + 1;
    const homefloor = Number($('#building .addHome .addHomefloor').val()) + 1;
    const homeNumber = $('#building .addHome .addHomeNumber').val();
    const homeToward = $('#building .addHome .addHomeToward').val();
    const homeArea = $('#building .addHome .addHomeArea').val();
    const homeWithin = $('#building .addHome .addHomeWithin').val();
    const homePublic = $('#building .addHome .addHomePublic').val();
    const room = $('#building .addHome .room').val() + '室';
    const hall = $('#building .addHome .hall').val() + '厅';
    const guard = $('#building .addHome .guard').val() + '卫';
    const apartment = room + hall + guard;

    console.log(
        homeUnit
        , homefloor
        , homeNumber
        , homeToward
        , homeArea
        , homeWithin
        , homePublic
        , apartment);
    ajax({
        url: '/building/addHome',
        type: 'POST',
        data: {
            homeUnit
            , homefloor
            , homeNumber
            , homeToward
            , homeArea
            , homeWithin
            , homePublic
            , apartment
        },
        success(res) {
            if(res.code==500){
                alert('不允许重复新增房屋')
            }else{
                alert('新增房屋成功')
            }
        }
    })
})

